import { ref, reactive } from 'vue'
import { useTransition } from '@vueuse/core'
import { listTeamApi } from '../../api/team'
import { statisticTotalFlowApi, statisticTeamTotalRiskRecordApi } from '../../api/statistic'

const teams = reactive({ arr: [] })
const currentTeam = ref()
const dayRiskRecordNum = ref(0)
const weekRiskRecordNum = ref(0)
const dayRiskRecordNumT = useTransition(dayRiskRecordNum, { duration: 1000 })
const weekRiskRecordNumT = useTransition(weekRiskRecordNum, { duration: 1000 })
const dayTotalFlow = ref(0)
const weekTotalFlow = ref(0)
const dayTotalFlowT = useTransition(dayTotalFlow, { duration: 1000 })
const weekTotalFlowT = useTransition(weekTotalFlow, { duration: 1000 })

async function getTeamList() {
  const { data: res } = await listTeamApi(1, 0x7fffffff)
  teams.arr = res.data.records.map((item) => {
    return {
      id: item.id,
      name: item.name
    }
  })
  currentTeam.value = teams.arr[0] ? teams.arr[0].id : null
}
async function getStatisticRiskRecord() {
  const { data: res } = await statisticTeamTotalRiskRecordApi(currentTeam.value)
  dayRiskRecordNum.value = res.data.dayRiskRecordNum
  weekRiskRecordNum.value = res.data.weekRiskRecordNum
}
async function getStatisticTotalFlow() {
  const { data: res } = await statisticTotalFlowApi(currentTeam.value)
  dayTotalFlow.value = res.data.dayTotalFlow
  weekTotalFlow.value = res.data.weekTotalFlow
}
function changeTeam() {
  getStatisticRiskRecord()
  getStatisticTotalFlow()
}
export const useStatistic = () => {
  return {
    teams,
    currentTeam,
    dayRiskRecordNumT,
    weekRiskRecordNumT,
    dayTotalFlowT,
    weekTotalFlowT,
    changeTeam,
    getTeamList,
    getStatisticRiskRecord,
    getStatisticTotalFlow
  }
}
